package com.define.ssh.service.impl;

import java.io.Serializable;
import java.util.Arrays;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.UUID;

import javax.annotation.Resource;

import org.hibernate.Criteria;
import org.hibernate.Query;
import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.springframework.transaction.annotation.Transactional;

import com.define.ssh.dao.BaseDao;
import com.define.ssh.model.InterfaceIp;
import com.define.ssh.model.TbLog;
import com.define.ssh.service.BaseService;
import com.define.ssh.service.TbLogService;
import com.define.ssh.util.InterfaceIpData;
import com.define.ssh.util.Pager;
import com.sxit.ecwx.core.utils.Formatu;
import com.sxit.ecwx.model.bean.BasePushMsg;
import com.sxit.ecwx.service.imp.ConnectServer;

/**
 * Service实现类 - Service实现类基类
 */

@Transactional
public class BaseServiceImpl<T, PK extends Serializable> implements
		BaseService<T, PK> {

	private BaseDao<T, PK> baseDao;
	@Resource(name = "tbLogServiceImpl")
	private TbLogService tbLogService;// 日志记录

	public BaseDao<T, PK> getBaseDao() {
		return baseDao;
	}

	public void setBaseDao(BaseDao<T, PK> baseDao) {
		this.baseDao = baseDao;
	}

	@Override
	@Transactional(readOnly = true)
	public T get(PK id) {
		return baseDao.get(id);
	}

	@Override
	@Transactional(readOnly = true)
	public T load(PK id) {
		return baseDao.load(id);
	}

	@Override
	@Transactional(readOnly = true)
	public List<T> getAllList() {
		return baseDao.getAllList();
	}

	@Override
	@Transactional(readOnly = true)
	public Long getTotalCount() {
		return baseDao.getTotalCount();
	}

	@Override
	@Transactional
	public PK save(T entity) {
		return baseDao.save(entity);
	}

	@Override
	@Transactional
	public void update(T entity) {
		baseDao.update(entity);
	}

	@Override
	@Transactional
	public void delete(T entity) {
		baseDao.delete(entity);
	}

	@Override
	@Transactional
	public void delete(PK id) {
		baseDao.delete(id);
	}

	@Override
	@Transactional
	public void delete(PK[] ids) {
		baseDao.delete(ids);
	}

	@Override
	@Transactional(readOnly = true)
	public void flush() {
		baseDao.flush();
	}

	@Override
	@Transactional(readOnly = true)
	public void evict(Object object) {
		baseDao.evict(object);
	}

	@Override
	@Transactional(readOnly = true)
	public void clear() {
		baseDao.clear();
	}

	@Override
	@Transactional(readOnly = true)
	public Pager findPager(Pager pager) {
		return baseDao.findPager(pager);
	}

	@Override
	@Transactional(readOnly = true)
	public Pager findPager(Pager pager, Criterion... criterions) {
		return baseDao.findPager(pager, criterions);
	}

	@Override
	@Transactional(readOnly = true)
	public Pager findPager(Pager pager, Order... orders) {
		return baseDao.findPager(pager, orders);
	}

	@Override
	@Transactional(readOnly = true)
	public Pager findPager(Pager pager, Criteria criteria) {
		return baseDao.findPager(pager, criteria);
	}

	/**
	 * 获取所有对象的集合
	 * 
	 * @param hashMap
	 *            参数值
	 * @return
	 */
	@Override
	public List<T> getAllListByParameter(HashMap<String, Object> hashMap) {
		return baseDao.getAllListByParameter(hashMap);
	}

	/**
	 * 获取所有对象的集合
	 * 
	 * @param hashMap
	 *            参数值
	 * @param orderByName
	 *            排序字段
	 * @return
	 */
	public List<T> getAllListByParameter(HashMap<String, Object> hashMap,
			String orderByName) {
		return baseDao.getAllListByParameter(hashMap, orderByName);
	}

	/**
	 * 返回符合条件的记录列表
	 * 
	 * @param dc
	 * @return list
	 */
	@Override
	public List findByCriteria(final DetachedCriteria dc) {
		return baseDao.findByCriteria(dc);
	}

	/**
	 * 搜索指定数量的数据
	 * 
	 * @param dc
	 * @param count
	 * @return
	 */
	@Override
	public List findByCriteria(final DetachedCriteria dc, final int count) {
		return baseDao.findByCriteria(dc, count);
	}
	
	
	public int sendMsg(byte[] by,int commond)
	{
		try {
		InterfaceIpData inetegerIp = InterfaceIpData.getInstance();
		List<InterfaceIp> Iplist = inetegerIp.getCommonMap().get(commond + "");
		for (InterfaceIp ii : Iplist) {
			UUID uuid = UUID.randomUUID();
			System.out.println("uuid=="+uuid.toString());
			System.out.println("ip=="+ii.getIp());
			System.out.println("port=="+ii.getPort());
			try {
				BasePushMsg bpm = new BasePushMsg(uuid.toString(),commond,by);
				ConnectServer cs = new ConnectServer();
				cs.connect2Core(ii.getIp(), Integer.parseInt(ii.getPort()));
				int result = cs.send2Core(bpm.getBy());
				if (result!=0) {
					// 重发一下
					System.out.println("重发一下==");
					bpm = new BasePushMsg(uuid.toString(), commond, by);
					cs = new ConnectServer();
					cs.connect2Core(ii.getIp(),Integer.parseInt(ii.getPort()));
					result = cs.send2Core(bpm.getBy());
					if(result!=0)
					{
						TbLog tl = new TbLog();
						tl.setModule("接口");
						tl.setUsername(commond + "");
						tl.setLcontent("uuid为" + uuid.toString() + ",更新内容为：");
						tl.setCreateDate(new Date());
						tl.setLtype( "");// 系统操作
						tl.setIpaddr(ii.getIp());
						tl.setRemark("操作失败");
						tbLogService.save(tl);
					}
				}
			} catch (Exception e) {
				TbLog tl = new TbLog();
				tl.setModule("接口");
				tl.setUsername(commond + "");
				tl.setLcontent("uuid为" + uuid.toString() + ",更新内容为：");
				tl.setCreateDate(new Date());
				tl.setLtype( "");// 系统操作
				tl.setIpaddr(ii.getIp());
				tl.setRemark(e.getMessage());
				tbLogService.save(tl);
				return 0;
				// 存表名，存最后一次时间
			}
		}
	} catch (Exception e) {
		TbLog tl = new TbLog();
		tl.setModule("接口");
		tl.setUsername(commond + "");
		tl.setLcontent( "操作接口为："+commond);
		tl.setCreateDate(new Date());
		tl.setLtype("");// 系统操作
		tl.setIpaddr("");
		tl.setRemark(e.getMessage());
		tbLogService.save(tl);
		return 0;
	}
		return 1;
	}

	/**
	 * 跟服务器通讯接口
	 * 
	 * @param modfiytime
	 * @param table
	 * @param commond
	 * @return
	 */
	public int connectInterfce(String table, int commond,int time) {
		try {
			
			byte[] by = new byte[4 + table.length()];
			System.arraycopy(Formatu.int2byte(time), 0, by,0, 4); /* 表记录上一次最新时间. */
			System.arraycopy(table.getBytes(), 0, by, 4, table.length()); /* 表名 */
			// 根据命令取ip与端口列表
			InterfaceIpData inetegerIp = InterfaceIpData.getInstance();
			List<InterfaceIp> Iplist = inetegerIp.getCommonMap().get(commond + "");
			for (InterfaceIp ii : Iplist) {
				UUID uuid = UUID.randomUUID();
				System.out.println("uuid=="+uuid.toString());
				System.out.println("ip=="+ii.getIp());
				System.out.println("port=="+ii.getPort());
				try {
					BasePushMsg bpm = new BasePushMsg(uuid.toString(),commond,by);
					ConnectServer cs = new ConnectServer();
					cs.connect2Core(ii.getIp(), Integer.parseInt(ii.getPort()));
					int result = cs.send2Core(bpm.getBy());
					if (result!=0) {
						// 重发一下
						System.out.println("重发一下==");
						bpm = new BasePushMsg(uuid.toString(), commond, by);
						cs = new ConnectServer();
						cs.connect2Core(ii.getIp(),Integer.parseInt(ii.getPort()));
						result = cs.send2Core(bpm.getBy());
						if(result!=0)
						{
							TbLog tl = new TbLog();
							tl.setModule("接口");
							tl.setUsername(commond + "");
							tl.setLcontent("uuid为" + uuid.toString() + ",更新时间为："
									+ time + ",表名为：" + table);
							tl.setCreateDate(new Date());
							tl.setLtype(time + "");// 系统操作
							tl.setIpaddr(ii.getIp());
							tl.setRemark("更新失败");
							tbLogService.save(tl);
						}
					}
				} catch (Exception e) {
					TbLog tl = new TbLog();
					tl.setModule("接口");
					tl.setUsername(commond + "");
					tl.setLcontent("uuid为" + uuid.toString() + ",更新时间为："
							+ time + ",表名为：" + table);
					tl.setCreateDate(new Date());
					tl.setLtype(time + "");// 系统操作
					tl.setIpaddr(ii.getIp());
					tl.setRemark(e.getMessage());
					tbLogService.save(tl);
					// 存表名，存最后一次时间
				}
			}
		} catch (Exception e) {
			TbLog tl = new TbLog();
			tl.setModule("接口");
			tl.setUsername(commond + "");
			tl.setLcontent( "表名为：" + table+",modfiytime"+time);
			tl.setCreateDate(new Date());
			tl.setLtype("");// 系统操作
			tl.setIpaddr("");
			tl.setRemark(e.getMessage());
			tbLogService.save(tl);
		}
		return 0;
	}
	
	
	
	
	
	
	
	

	
	/**
	 * 
	 * @param query
	 * @return list
	 */
	@Override
	public List findByQuery(final String query) {
		return baseDao.findByQuery(query);
	}

	/**
	 * 
	 * @param query
	 * @param parameter
	 * @return list
	 */
	@Override
	public List findByQuery(final String querySql, final Object parameter) {
		return baseDao.findByQuery(querySql, parameter);
	}

	/**
	 * @param query
	 * @param parameter
	 * @return
	 */
	@Override
	public List findByQuery(final String querySql, final Object[] parameter) {
		return baseDao.findByQuery(querySql, parameter);
	}

	
	

	public Date getUpdateTime(String table) {
		return baseDao.getUpdateTime(table);
	}
	
	public  Date getUpdateFiled(String table,String filed)
	{
		return baseDao.getUpdateFiled(table, filed);
	}

	public static void main(String[] args) {

//		String table = "user";
//		long modfiytime = 0l; // 在更新之前要取一下
//		Long time = System.currentTimeMillis() / 1000;
//		UUID uuid = UUID.randomUUID();
//		byte[] by = new byte[40 + table.length()];
//		System.arraycopy(uuid.toString().getBytes(), 0, by, 0, 36); /* 事务ID. */
//		System.arraycopy(Formatu.int2byte(time.intValue()), 0, by, 36, 4); /*
//																			 * 表记录上一次最新时间
//																			 * .
//																			 */
//		System.arraycopy(table.getBytes(), 0, by, 40, table.length()); /* 表名 */
//		System.out.println(Arrays.toString(by));
		
		byte[] b=new byte[]{0,0,12,20};
		System.out.println(Formatu.byte2int(b, 0));

	}

}